﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class ManageSubjects : System.Web.UI.Page
{
    DataTable dt;
    static ArrayList arrNewTopicName = new ArrayList();
    static ArrayList arrTopicName = new ArrayList();
    static ArrayList arrTopicId = new ArrayList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            arrTopicId.Clear();
            arrTopicName.Clear();
            arrNewTopicName.Clear();
            boundGrid();
        }
    }

    private void boundGrid()
    {
        dt = new DataTable();
        dt = AdminCreateTestDAL.GetSubjects();
        dt.Columns.Add("IsAct", typeof(System.String));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            if (dt.Rows[i][2].ToString() == "1")
                dt.Rows[i]["IsAct"] = "Yes";
            else
                dt.Rows[i]["IsAct"] = "No";
        }
        gvSubject.DataSource = dt;
        gvSubject.DataBind();
    }

    private void clearAll()
    {
        txtSubName.Text = "";
        txtAddEditTopic.Text = "";
        lblTopic.Items.Clear();
        gvSubject.SelectedIndex = -1;
        arrTopicId.Clear();
        arrTopicName.Clear();
        arrNewTopicName.Clear();
        btnAddEdit.Text = "Add";
        chkIsActive.Checked = false;
        lablError.Text = "";
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearAll();
    }

    protected void btnAddEdit_Click(object sender, EventArgs e)
    {
        bool flag = true;
        for (int i = 0; i < lblTopic.Items.Count; i++)
        {
            if (lblTopic.Items[i].Text.ToString() == txtAddEditTopic.Text)
            {
                flag = false;
                break;
            }
        }

        if (flag)
        {
            if ((lblTopic.SelectedIndex == -1) && (gvSubject.SelectedIndex == -1))
            {

                ListItem li = new ListItem();
                if (txtAddEditTopic.Text == "")
                {
                    lablError.Text = "Please Enter New Topic";
                    txtAddEditTopic.Focus();
                }
                else
                {
                    arrTopicName.Add(txtAddEditTopic.Text);
                    li.Text = txtAddEditTopic.Text;
                    li.Value = txtAddEditTopic.Text;
                    lblTopic.Items.Add(li);
                    txtAddEditTopic.Text = "";
                }
            }
            else
            {
                if ((gvSubject.SelectedIndex == -1) && (lblTopic.SelectedIndex != -1))
                {
                    arrTopicName.Remove(lblTopic.SelectedItem.Text);
                    lblTopic.Items.Remove(lblTopic.SelectedItem.Text);
                    ListItem li = new ListItem();
                    arrTopicName.Add(txtAddEditTopic.Text);
                    li.Text = txtAddEditTopic.Text;
                    li.Value = txtAddEditTopic.Text;
                    lblTopic.Items.Add(li);
                    txtAddEditTopic.Text = "";
                }
                else
                {
                    if (lblTopic.SelectedIndex == -1)
                    {
                        ListItem li = new ListItem();
                        arrNewTopicName.Add(txtAddEditTopic.Text);
                        li.Text = txtAddEditTopic.Text;
                        li.Value = txtAddEditTopic.Text;
                        lblTopic.Items.Add(li);
                        txtAddEditTopic.Text = "";
                    }
                    else
                    {
                        lblTopic.Items.RemoveAt(lblTopic.SelectedIndex);
                        ListItem li = new ListItem();
                        arrTopicName.Add(txtAddEditTopic.Text);
                        li.Text = txtAddEditTopic.Text;
                        li.Value = txtAddEditTopic.ToolTip;
                        lblTopic.Items.Add(li);
                        txtAddEditTopic.Text = "";
                        arrTopicId.Add(txtAddEditTopic.ToolTip);
                    }
                }
            }
        }

        else
        {

            lablError.Text = "Topic already present, Please enter diffrent topic name";
            txtAddEditTopic.Text = "";
            txtAddEditTopic.Focus();
        }
    }

    protected void lblTopic_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnAddEdit.Text = "Edit";
        txtAddEditTopic.Text = lblTopic.SelectedItem.Text;
        if (gvSubject.SelectedIndex != -1)
            txtAddEditTopic.ToolTip = lblTopic.SelectedItem.Value;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int isActive;
        if (lblTopic.Items.Count == 0)
        {
            lablError.Text = "Please Enter Topics";
            txtAddEditTopic.Focus();
        }
        else
        {
            if (gvSubject.SelectedIndex == -1)
            {
                string allTopics = "";

                for (int i = 0; i < arrTopicName.Count; i++)
                    allTopics += arrTopicName[i].ToString() + "|";
                if (chkIsActive.Checked == true)
                    isActive = 1;
                else
                    isActive = 0;
                AdminCreateTestDAL.ManngeSubject(txtSubName.Text, allTopics, isActive);
                clearAll();
                lablError.Text = "Subject information inserted successfully";
            }
            else
            {
                string allTopics = "";
                string allTopicId = "";
                string allNewTopics = "";
                if (chkIsActive.Checked == true)
                    isActive = 1;
                else
                    isActive = 0;

                for (int i = 0; i < arrTopicId.Count; i++)
                    allTopicId += arrTopicId[i].ToString() + "|";

                for (int i = 0; i < arrTopicName.Count; i++)
                    allTopics += arrTopicName[i].ToString() + "|";

                for (int i = 0; i < arrNewTopicName.Count; i++)
                    allNewTopics += arrNewTopicName[i].ToString() + "|";


                AdminCreateTestDAL.ManngeSubjectUpdation(Convert.ToInt32(txtSubName.ToolTip), txtSubName.Text, allTopicId, allTopics, isActive, allNewTopics);
                clearAll();
                lablError.Text = "Subject information modified successfully";
            }
            boundGrid();
        }
        arrTopicName.Clear();
        arrNewTopicName.Clear();
        btnAddEdit.Text = "Add";
    }

    protected void gvSubject_SelectedIndexChanged(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(gvSubject.SelectedDataKey.Value);
        dt = AdminCreateTestDAL.GetSubjectInfoById(id);
        txtSubName.Text = dt.Rows[0][0].ToString();
        txtSubName.ToolTip = id.ToString();
        if (dt.Rows[0]["IsActive"].ToString() == "1")
            chkIsActive.Checked = true;
        else
            chkIsActive.Checked = false;
        if (dt.Rows[0]["topicName"].ToString() != "null")
        {
            lblTopic.Items.Clear();
            arrTopicId.Clear();
            arrTopicName.Clear();
            arrNewTopicName.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ListItem li = new ListItem();
                li.Text = dt.Rows[i][1].ToString();
                li.Value = dt.Rows[i][2].ToString();
                lblTopic.Items.Add(li);
            }
        }
        else
            lblTopic.Items.Clear();
    }

    protected void txtSubName_TextChanged(object sender, EventArgs e)
    {
        bool flag = true;
        if (gvSubject.SelectedIndex == -1)
        {
            dt = new DataTable();
            dt = AdminCreateTestDAL.GetSubjects();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i][0].ToString().ToLower() == txtSubName.Text.ToLower())
                {
                    flag = false;
                    break;
                }
            }
            if (flag == false)
            {
                lablError.Text = "Subject name already exists, please choose another subject name";
                txtSubName.Text = "";
                txtSubName.Focus();
            }
            else
            {
                lablError.Text = "";
            }
        }
    }

    protected void gvSubject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSubject.PageIndex = e.NewPageIndex;
        boundGrid();
        gvSubject.DataBind();
    }

    
}